//Pietras93OS
//String
//pietras93@gmail.com
//


#include "include/string.h"

void *memset(void *dst, int val, unsigned count)
{
   unsigned short *temp = (unsigned short *)dst;
   for( ; count != 0; count--)
      *temp++ = val;
   return dst;
}

void *memcpy(void *dst_ptr, const void *src_ptr, unsigned count)
{
   void *ret_val = dst_ptr;
   const char *src = (const char *)src_ptr;
   char *dst = (char *)dst_ptr;
   for(; count != 0; count--)
      *dst++ = *src++;
   return ret_val;
}

void *memmove(void *dst_void, void *src_void, unsigned length)
{
   char *dst = dst_void;
   const char *src = src_void;
   if (src < dst && dst < src + length)
   {
      /* Have to copy backwards */
      src += length;
      dst += length;
      while (length--)
      {
         *--dst = *--src;
      }
   }
   else
   {
      while (length--)
      {
         *dst++ = *src++;
      }
   }
   return dst_void;
}

int strlen(const char *s1)
{
   const char *start = s1;
   while(*s1)
     s1++;
   return s1 - start;
}

char *strcpy(char *s1, const char *s2)
{
   while(*s1++ = *s2++);
   return s1;
}

/*char *explode(char delim, char *src)
{
	int i, j;//, k;
	char res;
	j = 0;
	//k = 0;
	for(i=0; strlen(src); i++)
	{
		if(strcmp(src[i],delim) == 0)
			j++;
		else {
			res[j][i] = src[i];
		}
	}
	return res;
}*/

char *strtok_r(char *s, const char *delim, char **last)
{
        char *spanp;
        int c, sc;
        char *tok;


        if (s == NULL && (s = *last) == NULL)
                return (NULL);

        /*
         * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
         */
cont:
        c = *s++;
        for (spanp = (char *)delim; (sc = *spanp++) != 0;) {
                if (c == sc)
                        goto cont;
        }

        if (c == 0) {           /* no non-delimiter characters */
                *last = NULL;
                return (NULL);
        }
        tok = s - 1;

        /*
         * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
         * Note that delim must have one NUL; we stop if we see that, too.
         */
        for (;;) {
                c = *s++;
                spanp = (char *)delim;
                do {
                        if ((sc = *spanp++) == c) {
                                if (c == 0)
                                        s = NULL;
                                else
                                        s[-1] = 0;
                                *last = s;
                                return (tok);
                        }
                } while (sc != 0);
        }
        /* NOTREACHED */
}

char *strtok(char *s, const char *delim)
{
        static char *last;

        return strtok_r(s, delim, &last);
}

char *strncat(char *s1, char *s2, unsigned n)
{
   char *s = s1;
   while(*s1)
     s1++;
   while(n-- != 0 && (*s1++ = *s2++))
   {
      if(n == 0)
        *s1 = '\0';
   }
   return s;
}

char *strncpy(char *dst, char *src, unsigned n)
{
   char *dscan;
   char *sscan;
   unsigned count;
   dscan = dst;
   sscan = src;
   count = n;
   while (count > 0)
   {
      --count;
      if((*dscan++ = *sscan++) == '\0')
        break;
   }
   while (count-- > 0)
      *dscan++ = '\0';
   return dst;
}

int strcmp(char *s1, char *s2)
{
   while (*s1 != '\0' && *s1 == *s2)
   {
      s1++;
      s2++;
   }
   return (*(unsigned char *) s1) - (*(unsigned char *) s2);
}

char *strcat(char *s1, const char *s2)
{
   char *s = s1;
   while(*s1)
      s1++;
   while(*s1++ = *s2++);
   return s;
}

int strncmp(char *s1, char *s2, unsigned n)
{
   if(n == 0)
      return 0;
   while(n-- != 0 && *s1 == *s2)
   {
      if(n == 0 || *s1 == '\0' || *s2 == '\0')
        break;
      s1++;
      s2++;
   }
   return (*(unsigned char *) s1) - (*(unsigned char *) s2);
}

int atoi(unsigned char *ch)
{
    int i = strlen(ch);
    int j,k, step=0, total=0;
    for( j = 0; j < i ; j++ )
    {
       k = (((int)ch[j])-48) * ( 10 * step );
       total = total + k;
       step++;
    }
    return total;
}

int instr(char *s1, char s2)
{
    int c=0;
    while(*s1 != '\0')
    {
       s1++;
       c++;
       if(*s1 == s2) return c;
    }
    return 0;
}

void reverse(char s[])
{
    int i, j;
    char c;

    for (i = 0, j = strlen(s)-1; i<j; i++, j--) {
        c = s[i];
        s[i] = s[j];
        s[j] = c;
    }
}
 
 char itoa(int n, char s[])
{
    int i, sign;

    if ((sign = n) < 0)  /* record sign */
        n = -n;          /* make n positive */
    i = 0;
    do {       /* generate digits in reverse order */
        s[i++] = n % 10 + '0';   /* get next digit */
    } while ((n /= 10) > 0);     /* delete it */
    if (sign < 0)
        s[i++] = '-';
    s[i] = '\0';
    reverse(s);
	return s;
}
